package com.jnshu.task.annotion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.jnshu.task.app.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class HelloMybatis {
    public static void main(String[] args) {
        //mybatis的配置文件
        String resource = "src/main/resources/sqlMapConfig1.xml";
        /**
         * 然后把这个配置文件转成InputStream给SqlSessionFactory
         */
        InputStream is = null;
        try {
            is = new FileInputStream(resource);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sessionFactory.openSession();
        /**
         * 映射sql的标识字符串，
         * com.knight.main.mapperr是mapper.xml文件中mapper标签的namespace属性的值，
         * name是select标签的id属性值，通过select标签的id属性值就可以找到要执行的SQL
         */
        String statement = "com.jnshu.task.mapper.UserMapper.findUserById";//映射sql的标识字符串
        //执行查询返回结果
        User user = session.selectOne(statement, 1);
        System.out.println(user);
    }
}
